/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ebdesk.controller.sppt;
import com.ebdesk.controller.dhkp.Lib;
import com.java4less.textprinter.*;
import com.java4less.textprinter.ports.FilePort;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

/**
 *
 * @author Ikmal Syifai
 */
public class Print {
    FilePort port;
    TextPrinter printer;
    boolean done;
    int blocklength;
    String tanggalpenetapan, tempatpembayaran;
    private SPPT sppt = new SPPT();
    
    public Print() {
       
    }
    
    public Print(String portname) {        
        try {
            this.port = new FilePort(portname);
            this.printer = PrinterFactory.getPrinter("PLAIN");            
            
            tanggalpenetapan = sppt.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'tanggal_penetapan'").toString();
            tempatpembayaran = sppt.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'tempat_pembayaran'").toString();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void setBlockLength(int length){
        blocklength = length;
    }
    
    public void doPrint(final String data[][], final JProgressBar pb, final int mode) throws Exception{        
        final int[][] coord = sppt.getCoordinate();
        
        SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground(){
                try {
                    int count;
                    int length;
                    
                    JobProperties job = printer.getDefaultJobProperties();
                    TextProperties p = printer.getDefaultTextProperties();
                    boolean neednewpage = false;

                    job.pitch = 12;
                    job.cols = 300;
                    job.rows = 45;
                    port.setTimeout(100000);

                    printer.startJob(port, job);                       

                    length = data.length;
                    setBlockLength(length);
                    
                    for(count = 0;count<length;count++){
                        publish(new Integer(count));
                        
                        if(neednewpage){
                            printer.newPage();
                            neednewpage = false;
                        }

                        if((mode==2 || mode==3) && count>0) printer.newPage();

                        //record genap (mulai dari 0)
                        if((mode==2) || (mode==0 && count%2==0) || (mode==1 & count%2!=0)){
                            printer.printString(data[count][27], coord[32][0], coord[32][1], p);//tahunpajak1
                            printer.printString(data[count][27], coord[33][0], coord[33][1], p);//tahunpajak2
                            printer.printString(Lib.FormatSPPT(data[count][0]), coord[13][0], coord[13][1], p);//nop
                            printer.printString(data[count][1], coord[15][0], coord[15][1], p);  //opalamat
                            printer.printString(data[count][21], coord[52][0], coord[52][1], p); //wpnama

                            printer.printString("RT :" + data[count][2], coord[25][0], coord[25][1], p); //oprt
                            printer.printString("RW :" + data[count][3], coord[26][0], coord[26][1], p); //oprw
                            printer.printString(data[count][22], coord[49][0], coord[49][1], p); //wpalamat

                            printer.printString(data[count][4], coord[18][0], coord[18][1], p); //opkelurahan
                            printer.printString("RT :" + data[count][23], coord[54][0], coord[54][1], p); //wprt
                            printer.printString("RW :" + data[count][24], coord[55][0], coord[55][1], p); //wprw

                            printer.printString(data[count][5], coord[16][0], coord[16][1], p); //opkecamatan
                            printer.printString(data[count][6].toUpperCase(), coord[20][0], coord[20][1], p); //opkota

                            printer.printString(data[count][25], coord[50][0], coord[50][1], p); //wpkelurahan
                            printer.printString(data[count][26], coord[51][0], coord[51][1], p); //wpkotakab                

                            printer.printString("BUMI", coord[46][0], coord[46][1], p);//tulisanbumi
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][7])), 10), coord[6][0], coord[6][1], p);//luasbumi
                            printer.printString(data[count][8], coord[2][0], coord[2][1], p);//kelasbumi
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][28]))), 17), coord[10][0], coord[10][1], p);//nilaiperm2tanah
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][9])), 20), coord[12][0], coord[12][1], p);//njopbumi

                            printer.printString("BANGUNAN", coord[45][0], coord[45][1], p);//tulisanbangunan
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][10])), 10), coord[5][0], coord[5][1], p);//luasbangunan
                            printer.printString(data[count][11], coord[1][0], coord[1][1], p);//kelasbangunan
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][29]))), 17), coord[9][0], coord[9][1], p);//nilaiperm2bangunan
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][12])), 20), coord[11][0], coord[11][1], p);//njopbangunan
                            
                            double bumibersama = Double.parseDouble(data[count][30]);
                            double bangunanbersama = Double.parseDouble(data[count][31]);
                            
                            if(bumibersama>0.0 || bangunanbersama>0.0){
                                printer.printString("BUMI BERSAMA", coord[57][0], coord[57][1], p);//tulisanbumibersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][30])), 10), coord[59][0], coord[59][1], p);//luasbumibersama
                                printer.printString(data[count][32], coord[61][0], coord[61][1], p);//kelasbumibersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][34]))), 17), coord[63][0], coord[63][1], p);//nilaiperm2tanah2
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][36])), 20), coord[65][0], coord[65][1], p);//njopbumibersama

                                printer.printString("BANGUNAN BERSAMA", coord[58][0], coord[58][1], p);//tulisanbangunanbersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][31])), 10), coord[60][0], coord[60][1], p);//luasbangunanbersama
                                printer.printString(data[count][33], coord[62][0], coord[62][1], p);//kelasbangunanbersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][35]))), 17), coord[64][0], coord[64][1], p);//nilaiperm2bangunan2
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][37])), 20), coord[66][0], coord[66][1], p);//njopbangunanbersama
                            }

                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][13])), 20), coord[23][0], coord[23][1], p);//opnjop

                            printer.printString("NJOPTKP :", coord[47][0], coord[47][1], p);//tulisannjoptkp
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][14])), 20), coord[24][0], coord[24][1], p);//opnjoptkp
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][15])), 20), coord[21][0], coord[21][1], p);//opnjkp
                            String persen_pengali = "";
                            persen_pengali = " "+ data[count][16] +" % X " ;//op tarif

                            printer.printString(persen_pengali +""+Lib.NumberFormat(Double.parseDouble(data[count][15])), coord[22][0], coord[22][1], p);//opnjkp2
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][17]))), 20), coord[30][0], coord[30][1], p);//pbbterhutang

                            double sppt_pengurang = Double.parseDouble(data[count][18]);
                            if(sppt_pengurang>0.0){
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][18])), 20), coord[29][0], coord[29][1], p);//pbbpengurang
                            }

                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][19]))), 20), coord[27][0], coord[27][1], p);//pbbharusdibayar
                            double pbb_harus_dibayar = Double.parseDouble(data[count][19]);

                            String terbilang = Lib.NumberToText(pbb_harus_dibayar).toUpperCase()+" RUPIAH";                
                            if(terbilang.length() >=79){
                                printer.printString(terbilang.substring(0, 79), coord[39][0], coord[39][1], p);//terbilang
                                printer.printString(terbilang.substring(79, terbilang.length()), coord[40][0], coord[40][1], p);//terbilang2
                            }else{
                                printer.printString(terbilang, coord[39][0], coord[39][1], p);//terbilang
                            }

                            Date jatuhtempo, penetapan;
                            DateFormat dateformat;     

                            dateformat = new SimpleDateFormat("yyyy-MM-dd");
                            jatuhtempo = (Date)dateformat.parse(data[count][20]);                    
                            penetapan = (Date)dateformat.parse(tanggalpenetapan);

                            printer.printString(Lib.dateFormatIndo2(jatuhtempo), coord[36][0], coord[36][1], p);//tanggaljatuhtempo                    
                            printer.printString(" "+data[count][6].toUpperCase()+", "+Lib.dateFormatIndo2(penetapan), coord[37][0], coord[37][1], p);//tanggalpenetapan

                            printer.printString(tempatpembayaran, coord[38][0], coord[38][1], p);//tempatpembayaran

                            printer.printString("NAMA WP ", coord[8][0], coord[8][1], p);//namawp
                            printer.printString(": ", coord[41][0], coord[41][1], p);//titikdua1
                            printer.printString(data[count][21], coord[53][0], coord[53][1], p);//wpnama2
                            printer.printString("DI TERIMA TGL ", coord[0][0], coord[0][1], p);//diterimatanggal
                            printer.printString("LETAK OBJEK PAJAK : KECAMATAN ", coord[4][0], coord[4][1], p);//letakop
                            printer.printString(data[count][5], coord[17][0], coord[17][1], p);//opkecamatan2
                            printer.printString("TANDA TANGAN", coord[35][0], coord[35][1], p);//tandatangan
                            printer.printString("KELURAHAN ", coord[3][0], coord[3][1], p);//kelurahan
                            printer.printString(data[count][4], coord[19][0], coord[19][1], p);//opkelurahan2
                            printer.printString("NOP ", coord[48][0], coord[48][1], p);//tulisannop
                            printer.printString(": ", coord[42][0], coord[42][1], p);//titikdua2
                            printer.printString(Lib.FormatSPPT(data[count][0]), coord[14][0], coord[14][1], p);//nop2
                            printer.printString("(........................)", coord[44][0], coord[44][1], p);//titiktitik
                            printer.printString("SPPT Tahun/Rp. ", coord[31][0], coord[31][1], p);//sppttahunrp
                            printer.printString(": ", coord[43][0], coord[43][1], p);//titikdua3
                            printer.printString(data[count][27], coord[34][0], coord[34][1], p);//tahunpajak3
                            printer.printString(" / "+Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][19]))), coord[28][0], coord[28][1], p);//pbbharusdibayar2
                            printer.printString("NAMA TERANG", coord[7][0], coord[7][1], p);//namaterang
                        }

                        if((mode==3) || (mode==0 && count%2!=0) || (mode==1 & count%2==0)){
                            int margin = coord[56][0];
                            printer.printString(data[count][27], coord[32][0], coord[32][1]+margin, p);//tahunpajak1
                            printer.printString(data[count][27], coord[33][0], coord[33][1]+margin, p);//tahunpajak2
                            printer.printString(Lib.FormatSPPT(data[count][0]), coord[13][0], coord[13][1]+margin, p);//nop
                            printer.printString(data[count][1], coord[15][0], coord[15][1]+margin, p);  //opalamat
                            printer.printString(data[count][21], coord[52][0], coord[52][1]+margin, p); //wpnama

                            printer.printString("RT :" + data[count][2], coord[25][0], coord[25][1]+margin, p); //oprt
                            printer.printString("RW :" + data[count][3], coord[26][0], coord[26][1]+margin, p); //oprw
                            printer.printString(data[count][22], coord[49][0], coord[49][1]+margin, p); //wpalamat

                            printer.printString(data[count][4], coord[18][0], coord[18][1]+margin, p); //opkelurahan
                            printer.printString("RT :" + data[count][23], coord[54][0], coord[54][1]+margin, p); //wprt
                            printer.printString("RW :" + data[count][24], coord[55][0], coord[55][1]+margin, p); //wprw

                            printer.printString(data[count][5], coord[16][0], coord[16][1]+margin, p); //opkecamatan
                            printer.printString(data[count][6].toUpperCase(), coord[20][0], coord[20][1]+margin, p); //opkota

                            printer.printString(data[count][25], coord[50][0], coord[50][1]+margin, p); //wpkelurahan
                            printer.printString(data[count][26], coord[51][0], coord[51][1]+margin, p); //wpkotakab                

                            printer.printString("BUMI", coord[46][0], coord[46][1]+margin, p);//tulisanbumi
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][7])), 10), coord[6][0], coord[6][1]+margin, p);//luasbumi
                            printer.printString(data[count][8], coord[2][0], coord[2][1]+margin, p);//kelasbumi
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][28]))), 17), coord[10][0], coord[10][1]+margin, p);//nilaiperm2tanah
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][9])), 20), coord[12][0], coord[12][1]+margin, p);//njopbumi

                            printer.printString("BANGUNAN", coord[45][0], coord[45][1]+margin, p);//tulisanbangunan
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][10])), 10), coord[5][0], coord[5][1]+margin, p);//luasbangunan
                            printer.printString(data[count][11], coord[1][0], coord[1][1]+margin, p);//kelasbangunan
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][29]))), 17), coord[9][0], coord[9][1]+margin, p);//nilaiperm2bangunan
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][12])), 20), coord[11][0], coord[11][1]+margin, p);//njopbangunan

                            double bumibersama = Double.parseDouble(data[count][30]);
                            double bangunanbersama = Double.parseDouble(data[count][31]);
                            
                            if(bumibersama>0.0 || bangunanbersama>0.0){
                                printer.printString("BUMI BERSAMA", coord[57][0], coord[57][1]+margin, p);//tulisanbumibersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][30])), 10), coord[59][0], coord[59][1]+margin, p);//luasbumibersama
                                printer.printString(data[count][32], coord[61][0], coord[61][1]+margin, p);//kelasbumibersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][34]))), 17), coord[63][0], coord[63][1]+margin, p);//nilaiperm2tanah2
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][36])), 20), coord[65][0], coord[65][1]+margin, p);//njopbumibersama

                                printer.printString("BANGUNAN BERSAMA", coord[58][0], coord[58][1]+margin, p);//tulisanbangunanbersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][31])), 10), coord[60][0], coord[60][1]+margin, p);//luasbangunanbersama
                                printer.printString(data[count][33], coord[62][0], coord[62][1]+margin, p);//kelasbangunanbersama
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][35]))), 17), coord[64][0], coord[64][1]+margin, p);//nilaiperm2bangunan2
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][37])), 20), coord[66][0], coord[66][1]+margin, p);//njopbangunanbersama
                            }
                            
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][13])), 20), coord[23][0], coord[23][1]+margin, p);//opnjop

                            printer.printString("NJOPTKP :", coord[47][0], coord[47][1]+margin, p);//tulisannjoptkp
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][14])), 20), coord[24][0], coord[24][1]+margin, p);//opnjoptkp
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][15])), 20), coord[21][0], coord[21][1]+margin, p);//opnjkp
                            String persen_pengali = "";
                            persen_pengali = " "+ data[count][16] +" % X " ;//op tarif

                            printer.printString(persen_pengali +""+Lib.NumberFormat(Double.parseDouble(data[count][15])), coord[22][0], coord[22][1]+margin, p);//opnjkp2
                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][17]))), 20), coord[30][0], coord[30][1]+margin, p);//pbbterhutang

                            double sppt_pengurang = Double.parseDouble(data[count][18]);
                            if(sppt_pengurang>0.0){
                                printer.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[count][18])), 20), coord[29][0], coord[29][1]+margin, p);//pbbpengurang
                            }

                            printer.printString(Lib.allignRight(Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][19]))), 20), coord[27][0], coord[27][1]+margin, p);//pbbharusdibayar
                            double pbb_harus_dibayar = Double.parseDouble(data[count][19]);

                            String terbilang = Lib.NumberToText(pbb_harus_dibayar).toUpperCase()+" RUPIAH";                
                            if(terbilang.length() >=79){
                                printer.printString(terbilang.substring(0, 79), coord[39][0], coord[39][1]+margin, p);//terbilang
                                printer.printString(terbilang.substring(79, terbilang.length()), coord[40][0], coord[40][1]+margin, p);//terbilang2
                            }else{
                                printer.printString(terbilang, coord[39][0], coord[39][1]+margin, p);//terbilang
                            }

                            Date jatuhtempo, penetapan;
                            DateFormat dateformat;     

                            dateformat = new SimpleDateFormat("yyyy-MM-dd");
                            jatuhtempo = (Date)dateformat.parse(data[count][20]);                    
                            penetapan = (Date)dateformat.parse(tanggalpenetapan);

                            printer.printString(Lib.dateFormatIndo2(jatuhtempo), coord[36][0], coord[36][1]+margin, p);//tanggaljatuhtempo                    
                            printer.printString(" "+data[count][6].toUpperCase()+", "+Lib.dateFormatIndo2(penetapan), coord[37][0], coord[37][1]+margin, p);//tanggalpenetapan

                            printer.printString(tempatpembayaran, coord[38][0], coord[38][1]+margin, p);//tempatpembayaran

                            printer.printString("NAMA WP ", coord[8][0], coord[8][1]+margin, p);//namawp
                            printer.printString(": ", coord[41][0], coord[41][1]+margin, p);//titikdua1
                            printer.printString(data[count][21], coord[53][0], coord[53][1]+margin, p);//wpnama2
                            printer.printString("DI TERIMA TGL ", coord[0][0], coord[0][1]+margin, p);//diterimatanggal
                            printer.printString("LETAK OBJEK PAJAK : KECAMATAN ", coord[4][0], coord[4][1]+margin, p);//letakop
                            printer.printString(data[count][5], coord[17][0], coord[17][1]+margin, p);//opkecamatan2
                            printer.printString("TANDA TANGAN", coord[35][0], coord[35][1]+margin, p);//tandatangan
                            printer.printString("KELURAHAN ", coord[3][0], coord[3][1]+margin, p);//kelurahan
                            printer.printString(data[count][4], coord[19][0], coord[19][1]+margin, p);//opkelurahan2
                            printer.printString("NOP ", coord[48][0], coord[48][1]+margin, p);//tulisannop
                            printer.printString(": ", coord[42][0], coord[42][1]+margin, p);//titikdua2
                            printer.printString(Lib.FormatSPPT(data[count][0]), coord[14][0], coord[14][1]+margin, p);//nop2
                            printer.printString("(........................)", coord[44][0], coord[44][1]+margin, p);//titiktitik
                            printer.printString("SPPT Tahun/Rp. ", coord[31][0], coord[31][1]+margin, p);//sppttahunrp
                            printer.printString(": ", coord[43][0], coord[43][1]+margin, p);//titikdua3
                            printer.printString(data[count][27], coord[34][0], coord[34][1]+margin, p);//tahunpajak3
                            printer.printString(" / "+Lib.NumberFormat(Math.ceil(Double.parseDouble(data[count][19]))), coord[28][0], coord[28][1]+margin, p);//pbbharusdibayar2
                            printer.printString("NAMA TERANG", coord[7][0], coord[7][1]+margin, p);//namaterang

                            if(mode==0 || mode==1) neednewpage = true;
                        }                                
                    }

                    done = true;
                    printer.endJob();
                }
                catch (TextPrinterException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }                    
                catch (ParseException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                return null;
            }
            
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
            
                pb.setVisible(false);                
            }
            
            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
//                System.out.println(x+" "+blocklength);
            }
        };
        
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();                
    }        
}
